<?php

use App\Support\Response;
use Hyperf\Utils\ApplicationContext;
use HyperfExt\Auth\Contracts\AuthManagerInterface;
use HyperfExt\Auth\Contracts\GuardInterface;
use HyperfExt\Auth\Contracts\StatefulGuardInterface;
use HyperfExt\Auth\Contracts\StatelessGuardInterface;
use Psr\Container\ContainerInterface;

if (!function_exists('auth')) {
    /**
     * Auth认证辅助方法
     * @param string|null $guard
     * @return GuardInterface|StatefulGuardInterface|StatelessGuardInterface
     */
    function auth(string $guard = null)
    {
        if (is_null($guard)) $guard = config('auth.default.guard');

        return make(AuthManagerInterface::class)->guard($guard);
    }
}

if (!function_exists('response')) {
    /**
     * Response 响应
     */
    function response()
    {
        return make(Response::class);
    }
}

if (!function_exists('redis')) {
    /**
     * redis 客户端示例
     * @return mixed|Redis
     */
    function redis()
    {
        return container()->get(Redis::class);
    }
}

if (!function_exists('error')) {
    /**
     * @param string $msg
     * @param int $error_code
     *
     * @param array|null $data
     *
     * @return false|string
     */
    function error(string $msg, int $error_code = 1, array $data = null): string
    {
        return json($msg, $error_code, $data);
    }
}

if (!function_exists('success')) {
    /**
     * @param string $msg
     * @param int $success_code
     * @param array|null $data
     *
     * @return string
     */
    function success(string $msg, int $success_code = 0, array $data = null): string
    {
        return json($msg, $success_code, $data);
    }
}

if (!function_exists('json')) {
    /**
     * @param string $msg
     * @param int $code
     * @param array|null $data
     *
     * @return string
     */
    function json(string $msg, int $code = 0, array $data = null): string
    {
        $succeedData = [
            'msg' => $msg,
            'code' => $code,
            'data' => $data
        ];

        return json_encode($succeedData, 256);
    }
}

if (!function_exists('container')) {
    /**
     * 容器示例
     * @return ContainerInterface
     */
    function container()
    {
        return ApplicationContext::getContainer();
    }
}